<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">用户角色权限管理</a>
          <a><cite>菜单&权限列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <button class="layui-btn layui-btn-sm" onclick="parent.xadmin.add_tab('新增节点','/admin/menu/add',false)"><i
                class="layui-icon"></i>添加
        </button>
        <button class="layui-btn layui-btn-warm layui-btn-sm" onclick="openAll();"><i class="layui-icon"></i>展开或折叠全部
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="reload()"><i
                class="layui-icon layui-icon-refresh"></i>刷新
        </button>
    </div>
    <div class="layui-card-body ">
        <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
    </div>
</div>
</body>
<script>
    var editObj = null, ptable = null, treeGrid = null, tableId = 'treeTable', layer = null, $ = null;
    layui.config({
        base: '/xadmin/lib/layui-v2.5.7/lay/modules/'
    }).extend({
        treeGrid: 'treeGrid',
    }).use(['jquery', 'treeGrid', 'layer', 'element'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        layui.element.render();
        treeGrid = layui.treeGrid;//很重要
        layer = layui.layer;
        ptable = treeGrid.render({
            id: tableId
            , elem: '#' + tableId
            , url: '/admin/menu/tree'
            , cellMinWidth: 100
            , idField: 'id'//必須字段
            , treeId: 'id'//树形id字段名称
            , treeUpId: 'pid'//树形父id字段名称
            , treeShowName: 'title'//以树形式显示的字段
            , heightRemove: [".dHead", 10]//不计算的高度,表格设定的是固定高度，此项不生效
            , height: '100%'
            , isFilter: false
            , iconOpen: false//是否显示图标【默认显示】
            , isOpenDefault: false//节点默认是展开还是折叠【默认展开】
            , loading: true
            , method: 'post'
            , isPage: false
            , cols: [
                [
                    // ,{type:'radio'}
                    {type: 'checkbox', sort: true}
                    , {type: 'numbers'}
                    , {field: 'title', width: 300, title: '菜单名称'}
                    , {field: 'prem', width: 300, title: '权限名称'}
                    , {field: 'icon', width: 300, title: '图标'}
                    , {field: 'sorts', width: 100, title: '排序'}
                    , {
                    width: 100, title: '是否显示', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        if (d.status) {
                            return '<a lay-event="status">' + '<input type="checkbox" lay-event="status-close" name="status" lay-skin="switch" lay-text="显示|隐藏" checked>' + '</a>';
                        } else {
                            return '<a lay-event="status">' + '<input type="checkbox" lay-event="status-open" name="status" lay-skin="switch" lay-text="显示|隐藏" >' + '</a>';
                        }
                    }
                }
                    , {
                    width: 100, title: '是否菜单', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        if (d.type) {
                            return '<a lay-event="menu">' + '<input type="checkbox" lay-event="menu-close" name="type" lay-skin="switch" lay-text="是|否" checked>' + '</a>';
                        } else {
                            return '<a lay-event="menu">' + '<input type="checkbox" lay-event="menu-open" name="type" lay-skin="switch" lay-text="是|否">' + '</a>';
                        }
                    }
                }
                    , {
                    width: 250, title: '操作', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        var editBtn = '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                        var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete">删除</a>';
                        return editBtn + delBtn;
                    }
                }
                ]
            ]
            , parseData: function (res) {//数据加载后回调
                return res;
            }
        });
        //监听单元格编辑
        treeGrid.on('tool(' + tableId + ')', function (obj) {
            if (obj.event === 'del') {//删除行
                del(obj);
            } else if (obj.event === "add") {//添加子节点的
                xadmin.open('添加', '/admin/menu/add?id=' + obj.data.id);
            } else if (obj.event === "edit") {//隐藏
                xadmin.open('编辑', '/admin/menu/edit?id=' + obj.data.id);
            } else if (obj.event === "status") {//隐藏
                ajax_update(obj.data.id, {status: !obj.data.status});
            } else if (obj.event === "menu") {//显示
                ajax_update(obj.data.id, {type: !obj.data.type});
            }
        });

    });

    function ajax_update(id, json) {
        $.ajax({
            url: '/admin/menu/ajax?id=' + id,
            method: 'post',
            data: json,
            dataType: 'JSON',
            success: function (res) {
                if (res.code == 0) {
                    layer.msg('修改成功', {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 5});
                    reload();
                }
            },
            error: function (data) {
                layer.msg('服务器繁忙', {icon: 5});
                reload();
            }
        })
    }

    function del(obj) {
        layer.confirm("你确定删除数据吗！", {icon: 3, title: '提示'},
            function (index) {//确定回调
                let data = {};
                data.id = obj.data.id;
                $.ajax({
                    url: '/admin/menu/delete',
                    method: 'post',
                    data: data,
                    dataType: 'JSON',
                    success: function (res) {
                        //基本演示
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                layer.close(index);
            }, function (index) {//取消回调
                layer.close(index);
            }
        );
    }

    function openAll() {
        var treedata = treeGrid.getDataTreeList(tableId);
        treeGrid.treeOpenAll(tableId, !treedata[0][treeGrid.config.cols.isOpen]);
    }

    function reload() {
        treeGrid.reload(tableId, {
            page: {
                curr: 1
            }
        });
    }
</script>
</html>